﻿using AngryManagementMayhem.Content.Textures.MainMenu;
using AngryManagementMayhem.Controller;
using AngryManagementMayhem.GameCode.Screens.MainMenuScreen.MainMenuObjects;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace AngryManagementMayhem.GameCode.Screens.MainMenuScreen
{
    public class CreateGameMode : Screen
    {
        private GameModeDescription _description;

        private IconButton _clickedButton;

        public override void LoadContent()
        {
            PositionGridBound = new TE_Vector2(Game.Options.ScreenSize.X, 0);
            AddAnimations(new PositionAnimation(new OvershootEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(0, 0), 1500, false, 0));

            AddChildren(new MenuBackground("Create a custom game"));

            AddChildren(new Label(MainMenuSpriteSheet.GetSprite, "Step 2 of 2")
            {
                PositionGridBound = new TE_Vector2(42.5f, 2),
                TextAlign = TE_Rectangle.Alignment.CenterRight
            });

            AddChildren(new Label(MainMenuSpriteSheet.GetSprite, "Select a map")
            {
                PositionGridBound = new TE_Vector2(2.5f, 5),
                TextAlign = TE_Rectangle.Alignment.TopLeft
            });

            _description = new GameModeDescription
                {
                    PositionGridBound = new TE_Vector2(15.5f, 5),
                    Visible = false,
                };
            AddChildren(_description);

            var ctf = new IconButton(new ImageContainer { Sprite = MainMenuSpriteSheet.GetSprite(MainMenuSpriteSheet.MapIcon.OfficeWars) })
                {
                    PositionGridBound = new TE_Vector2(5, 9)
                };
            ctf.OnEnter += position => DisplayDescription("Office wars",
                                                          "Game mode: Capture the flag",
                                                          "Ω Recommended players: 4",
                                                          "",
                                                          "Deck it out in this crazy 4v4",
                                                          "capture-the-flag style map",
                                                          "Do not let your opponents steal",
                                                          "your company's precious secret documents");
            ctf.OnLeave += position => HideDescription();
            ctf.OnClick += position => ClickMode(ctf, GameController.GameMode.CaptureTheFlag, GameController.MapList.OfficeWars, 8);
            if(GameController.Map == GameController.MapList.OfficeWars)
                ctf.Click();

            AddChildren(ctf);

            var previousButton = new IconButton(new ImageContainer { Sprite = MainMenuSpriteSheet.GetSprite(MainMenuSpriteSheet.SpriteList.Next), Mirror = ImageContainer.MirrorState.MirrorHorizontal })
            {
                PositionGridBound = new TE_Vector2(38, 26.5f)
            };
            previousButton.OnClick += i => ChangeScreen("createGame");

            var nextButton = new IconButton(new ImageContainer { Sprite = MainMenuSpriteSheet.GetSprite(MainMenuSpriteSheet.SpriteList.Ok) })
            {
                PositionGridBound = new TE_Vector2(40.5f, 26.5f)
            };
            nextButton.OnClick += i => ChangeScreen("selectTeams");

            AddChildren(previousButton, nextButton);

            base.LoadContent();
        }

        void ClickMode(IconButton newButton, GameController.GameMode mode, GameController.MapList map, int maxPlayers)
        {
            if (_clickedButton != null)
                _clickedButton.Click();

            newButton.Click();
            _clickedButton = newButton;

            GameController.Mode = mode;
            GameController.Map = map;
            GameController.MaxPlayers = maxPlayers;
        }

        void SaveMode()
        {
            
        }

        void DisplayDescription(string title, params string[] subtitle)
        {
            _description.ChangeText(title, subtitle);
            _description.Visible = true;
        }

        void HideDescription()
        {
            _description.Visible = false;
        }

        public override void Die(double timeToLive)
        {
            ClearAnimation();
            AddAnimations(new PositionAnimation(new OvershootEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(-Game.Options.ScreenSize.X, 0), timeToLive, false, 0));

            base.Die(timeToLive);
        }
    }
}